package com.wujp.common.template.tools.actuator;

import com.wujp.common.template.tools.context.BaseProcessContext;
import com.wujp.common.template.tools.exception.CheckException;
import com.wujp.common.template.tools.interfaces.TemplateActuator;
import com.wujp.common.template.tools.model.template.BaseTemplateModel;
import com.wujp.common.template.tools.model.action.AcionModel;
import com.wujp.common.template.tools.model.template.SerialTemplateModel;

import java.util.Iterator;
import java.util.List;

/**
 * @desc: 串行模型的执行器
 * @author: wujp
 * @create: 2023-01-18 09:58
 */
public class SerialTemplateActuator implements TemplateActuator<BaseProcessContext> {

    private SerialTemplateActionActuator actionActuator = new SerialTemplateActionActuator();

    @Override
    public void exec(BaseProcessContext context) {
        BaseTemplateModel baseTemplateModel = context.getBaseTemplateModel();
        //TODO 校验工作 checker接口实现
        if (!(baseTemplateModel instanceof SerialTemplateModel)) {
            throw new CheckException("执行器和模型不匹配。accoutor=SerialTemplateActuator");
        }
        SerialTemplateModel serialTemplateModel = (SerialTemplateModel) baseTemplateModel;
        List<AcionModel> actions = serialTemplateModel.getActions();
        Iterator<AcionModel> iterator = actions.iterator();
        while (iterator.hasNext()) {
            AcionModel action = iterator.next();
            actionActuator.exec(action, context);
        }
    }

}
